﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using Ooaavee.Scripting.Remoting;

namespace Ooaavee.Scripting
{
    /// <summary>
    /// A strongly typed collection of <see cref="Reference"/> objects.
    /// </summary>
    [Serializable]
    public sealed class References : Collection<Reference>, ICloneable
    {
        /// <summary>
        /// Returns the default references for a script.
        /// </summary>
        /// <returns>the default references</returns>
        internal static References GetDefault()
        {
            References references = new References();

            List<Assembly> assemblies = Reference.GetDefaultFrameworkAssemblies();
            foreach (Assembly assembly in assemblies)
            {
                Reference reference = Reference.CreateReference(assembly);
                references.Add(reference);                                
            }

            Reference reference2 = Reference.CreateReference(typeof(IRemoteScript).Assembly);
            reference2.IsOutsideGac = true;
            references.Add(reference2);
          
            return references;
        }

        /// <summary>
        /// Returns referenced assembly files.
        /// </summary>
        /// <returns>assembly files</returns>
        internal string[] GetAssemblyFiles()
        {
            return this.Select(reference => reference.AssemblyFile.FullName).ToArray();
        }

        /// <summary>
        /// Returns a deep copy of this object.
        /// </summary>
        /// <returns>a deep copy</returns>
        object ICloneable.Clone()
        {
            References clone = new References();

            foreach (Reference reference in this)
            {
                clone.Add((Reference)((ICloneable)reference).Clone());
            }

            return clone;
        }
    }
}